//
//  rankViewController.m
//  EasyFM
//
//  Created by Jackson on 16/8/8.
//  Copyright © 2016年 Changyun Liu. All rights reserved.
//

#import "rankViewController.h"
#import "RankModels.h"
#import "RankTableViewCell.h"
#import "FocusViewController.h"
#import "ProgramListViewController.h"
#import "OtherProgramViewController.h"
#import "OtherRankListModel.h"

@interface rankViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, copy) NSMutableArray *programArray;
@property (nonatomic, copy) NSMutableArray *programTitleArray;
@property (nonatomic, copy) NSMutableArray *anchorArray;
@property (nonatomic, copy) NSMutableArray *anchorTitleArray;
@property (nonatomic, copy) NSMutableArray *sectionArray;
@property (nonatomic, copy) NSMutableArray *listArray;
@property (nonatomic, copy) NSMutableArray *anchorUrlArray;

@end

@implementation rankViewController

- (NSMutableArray *)programArray
{
    if (!_programArray) {
        _programArray = [[NSMutableArray alloc] init];
    }
    return _programArray;
}

- (NSMutableArray *)programTitleArray
{
    if (!_programTitleArray) {
        _programTitleArray = [[NSMutableArray alloc] init];
    }
    return _programTitleArray;
}

- (NSMutableArray *)anchorArray
{
    if (!_anchorArray) {
        _anchorArray = [[NSMutableArray alloc] init];
    }
    return _anchorArray;
}

- (NSMutableArray *)anchorTitleArray
{
    if (!_anchorTitleArray) {
        _anchorTitleArray = [[NSMutableArray alloc] init];
    }
    return _anchorTitleArray;
}

- (NSMutableArray *)sectionArray
{
    if (!_sectionArray) {
        _sectionArray = [[NSMutableArray alloc] init];
    }
    return _sectionArray;
}

-(NSMutableArray *)listArray{
    if (!_listArray) {
        _listArray = [NSMutableArray arrayWithObjects:KHotRankURL,KMoreRankURL,KBuyRankURL,KShareRankURL,KNovelRankURL,KLanguageRankURL,KEducationRankURL,KEntertainmentRankURL,KMUsicRankURL,KBusinessRankURL,KNewsRankURL,KScienceRankURL,KHistoryRankURL,KPrefectURL,KWorkplaceURL, nil];
    }
    return _listArray;
}

-(NSMutableArray *)anchorArr{
    if (!_anchorUrlArray) {
        _anchorUrlArray = [NSMutableArray arrayWithObjects:KWomanRankURL,KManRankURL,KNewManRankURL,KAnchorRankURL, nil];
    }
    return _anchorUrlArray;
}



- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 153) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"RankTableViewCell" bundle:nil] forCellReuseIdentifier:@"rankCell"];
    
    [self.view addSubview:self.tableView];
    
    [self analyseData];
}

- (void)analyseData
{
    [NetWorkrequestManage requestWithType:GET url:KRankUrl parameters:nil finish:^(NSData *data) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        NSString *programTitle = dict[@"datas"][0][@"title"];
        NSString *anchorTitle = dict[@"datas"][1][@"title"];
        [self.sectionArray addObject:programTitle];
        [self.sectionArray addObject:anchorTitle];
        
        NSDictionary *headViewDict = dict[@"focusImages"][@"list"][0];
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight / 5)];
        [imgView sd_setImageWithURL:[NSURL URLWithString:headViewDict[@"pic"]] placeholderImage:[UIImage imageNamed:@"1004"]];
        
        
        NSArray *array1 = dict[@"datas"][0][@"list"];
        for (NSDictionary *dic in array1) {
            RankModels *model = [[RankModels alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.programArray addObject:model];
            
            NSArray *array = model.firstKResults;
            NSMutableArray *arr = [NSMutableArray array];
            for (NSDictionary *titleDic in array) {
                NSString *title = titleDic[@"title"];
                [arr addObject:title];
            }
            [self.programTitleArray addObject:arr];
        }
        
        NSArray *array2 = dict[@"datas"][1][@"list"];
        for (NSDictionary *dic in array2) {
            RankModels *model = [[RankModels alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.anchorArray addObject:model];
            
            NSArray *array = model.firstKResults;
            NSMutableArray *arr = [NSMutableArray array];
            for (NSDictionary *titleDic in array) {
                NSString *title = titleDic[@"title"];
                [arr addObject:title];
            }
            [self.anchorTitleArray addObject:arr];
        }

        
        dispatch_async(dispatch_get_main_queue(), ^{
            UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        
            self.headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight / 5)];
            [_headView addSubview:imgView];
            
            [self.headView addGestureRecognizer:tap];
            self.tableView.tableHeaderView = _headView;
            [self.tableView reloadData];
        });
        
    } error:^(NSError *error) {
        
    }];
   
}

- (void)tapAction
{
    FocusViewController *focusVC = [[FocusViewController alloc] init];
    [self.navigationController pushViewController:focusVC animated:YES];
}

#pragma mark --------------- tableView delegate ------------------

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.sectionArray[section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sectionArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.programArray.count;
    } else {
        return self.anchorArray.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RankTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rankCell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        [cell setupCellWithModel:self.programArray[indexPath.row] array:self.programTitleArray[indexPath.row]];
    } else {
        [cell setupCellWithModel:self.anchorArray[indexPath.row] array:self.anchorTitleArray[indexPath.row]];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProgramListViewController *programVC = [[ProgramListViewController alloc]init];
    OtherProgramViewController *OtherVC = [[OtherProgramViewController alloc]init];
    if (indexPath.section == 0) {
        RankModels *model = self.programArray[indexPath.row];
        OtherVC.titleStr = model.title;
        programVC.titleStr = model.title;
        if (indexPath.row == 0) {
            programVC.urlStr = KHotRankURL;
        }
        else if (indexPath.row == 1) {
            programVC.urlStr = KMoreRankURL;
        }
        else if (indexPath.row == 13) {
            programVC.urlStr = KPrefectURL;
        } else {
            OtherVC.urlStr = self.listArray[indexPath.row];
            OtherVC.indexC = indexPath.row;
            self.tabBarController.tabBar.hidden = YES;
            [self.navigationController pushViewController:OtherVC animated:YES];
            return;
        }
    } else{
        OtherRankListModel *model = self.anchorArray[indexPath.row];
        OtherVC.titleStr = model.title;
        OtherVC.indexC = 100;
        OtherVC.urlStr = self.anchorUrlArray[indexPath.row];
        self.tabBarController.tabBar.hidden = YES;
        [self.navigationController pushViewController:OtherVC animated:YES];
        return;
    }
    self.tabBarController.tabBar.hidden = YES;
    NSLog(@"%@", self.navigationController);
    [self.navigationController pushViewController:programVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
